import { WereWolfFirstDayPhase } from "@/werewolf/phases/firstDay";
import { WereWolfPhaseView } from "@/werewolf/phaseView";
import { world } from "@minecraft/server";
import { Dimensions } from "@sapi-game/constants";
import { Game } from "@sapi-game/main";
import { ScriptRunner } from "@sapi-game/Runner/scriptRunner";
import { DimensionIds, EntityTypeIds } from "@sapi-game/utils/vanila-data";
import { ShowRoleCampView } from "./showRole";

export class FirstDayCampView extends WereWolfPhaseView<WereWolfFirstDayPhase> {
    basePos = { x: -207, y: -49, z: -187 };
    isShowingRole: boolean = false;

    override async runPhaseView(r: ScriptRunner): Promise<void> {
        this.placeStructure("lrs_c");
        await r.wait(60);
        for (let i = 1; i <= 4; i++) {
            this.placeStructure(`lrs_c${i}`);
            this.players.playSound("mob.blaze.shoot");
            await r.wait(3);
            this.placeStructure("lrs_c");
            await r.wait(7);
        }
        await r.wait(10);
        this.placeStructure("lrs_c5");
        this.players.playSound("note.bell");
        this.listenForButton();
    }

    placeStructure(structure: string) {
        world.structureManager.place(
            structure,
            Dimensions.Overworld,
            this.basePos
        );
    }

    listenForButton() {
        this.subscribe(
            Game.events.buttonPush,
            (t) => {
                this.state.addComponent(ShowRoleCampView);
            },
            {
                loc: [-201, -46, -181],
                dimensionId: DimensionIds.Overworld,
                sourceType: EntityTypeIds.Player,
            }
        );
    }
}
